#include "frmserialport.h"
#include "ui_frmserialport.h"
#include <QMessageBox>
#include <QSerialPortInfo>
#include <QDebug>

frmserialport::frmserialport(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::frmserialport),
    mainWindow(qobject_cast<frmmain*>(parent->window())),
    isCommandMode(false),
    waitingForCommandModeResponse(false),
    waitingForStatusResponse(false),
    commandModeTimer(new QTimer(this)),
    statusTimer(new QTimer(this))
{
    ui->setupUi(this);

    if (!mainWindow) {
        qDebug() << "警告：mainWindow 指针无效，frmserialport 构造失败";
        return;
    }

    qDebug() << "frmserialport 构造，mainWindow:" << mainWindow;

    // 初始化端口列表
    updatePorts();
    ui->portComboBox->setCurrentIndex(-1); // 默认不选中
    ui->baudRateComboBox->setCurrentIndex(4); // 默认 115200

    // 设置定时器为单次触发
    commandModeTimer->setSingleShot(true);
    statusTimer->setSingleShot(true);
    connect(commandModeTimer, &QTimer::timeout, this, &frmserialport::on_commandModeTimeout);
    connect(statusTimer, &QTimer::timeout, this, &frmserialport::on_statusTimeout);

    // 初始化工作模式选项的实际值
    ui->modeComboBox->setItemData(0, 0); // 节能模式 -> 0
    ui->modeComboBox->setItemData(1, 1); // 正常模式1 -> 1
    ui->modeComboBox->setItemData(2, 2); // 正常模式2 -> 2
    ui->modeComboBox->setItemData(3, 3); // 正常模式3 -> 3
    ui->modeComboBox->setItemData(4, 4); // 应急模式 -> 4

    // 清理并连接信号
    disconnect(ui->setModeButton, &QPushButton::clicked, this, &frmserialport::on_setModeButton_clicked);
    disconnect(ui->queryModeButton, &QPushButton::clicked, this, &frmserialport::on_queryModeButton_clicked);
    disconnect(ui->querySelfCheckButton, &QPushButton::clicked, this, &frmserialport::on_querySelfCheckButton_clicked);
    disconnect(ui->queryAccelButton, &QPushButton::clicked, this, &frmserialport::on_queryAccelButton_clicked);
    disconnect(ui->setAccelButton, &QPushButton::clicked, this, &frmserialport::on_setAccelButton_clicked);
    disconnect(ui->queryTiltButton, &QPushButton::clicked, this, &frmserialport::on_queryTiltButton_clicked);
    disconnect(ui->setTiltButton, &QPushButton::clicked, this, &frmserialport::on_setTiltButton_clicked);
    disconnect(ui->queryNtripButton, &QPushButton::clicked, this, &frmserialport::on_queryNtripButton_clicked);
    disconnect(ui->setNtripButton, &QPushButton::clicked, this, &frmserialport::on_setNtripButton_clicked);

    connect(ui->setModeButton, &QPushButton::clicked, this, &frmserialport::on_setModeButton_clicked);
    connect(ui->queryModeButton, &QPushButton::clicked, this, &frmserialport::on_queryModeButton_clicked);
    connect(ui->querySelfCheckButton, &QPushButton::clicked, this, &frmserialport::on_querySelfCheckButton_clicked);
    connect(ui->queryAccelButton, &QPushButton::clicked, this, &frmserialport::on_queryAccelButton_clicked);
    connect(ui->setAccelButton, &QPushButton::clicked, this, &frmserialport::on_setAccelButton_clicked);
    connect(ui->queryTiltButton, &QPushButton::clicked, this, &frmserialport::on_queryTiltButton_clicked);
    connect(ui->setTiltButton, &QPushButton::clicked, this, &frmserialport::on_setTiltButton_clicked);
    connect(ui->queryNtripButton, &QPushButton::clicked, this, &frmserialport::on_queryNtripButton_clicked);
    connect(ui->setNtripButton, &QPushButton::clicked, this, &frmserialport::on_setNtripButton_clicked);

    if (mainWindow) {
        QSerialPort* serialPort = mainWindow->getSerialPort();
        connect(serialPort, &QSerialPort::errorOccurred, this, [this, serialPort](QSerialPort::SerialPortError error) {
            Q_UNUSED(error);
            if (mainWindow && mainWindow->getSerialPort() && mainWindow->getSerialPort() == serialPort) {
                updateMainResponse("串口错误: " + mainWindow->getSerialPort()->errorString());
                if (mainWindow->getSerialPort()->isOpen()) {
                    disconnect(serialPort, &QSerialPort::errorOccurred, 0, 0);
                    disconnect(serialPort, &QSerialPort::readyRead, 0, 0);
                    mainWindow->getSerialPort()->close();
                    ui->connectButton->setText("连接");
                    ui->enterCommandModeButton->setEnabled(false);
                    ui->exitCommandModeButton->setEnabled(false);
                    enableConfigControls(false);
                    mainWindow->setIsSerialConnected(false);
                    updateMainResponse("串口已断开（错误自动断开）");
                }
            }
        });
        connect(mainWindow, &frmmain::serialDataReceived, this, &frmserialport::on_serialDataReceived);
    }
}

frmserialport::~frmserialport()
{
    if (mainWindow && mainWindow->getSerialPort() && mainWindow->getSerialPort()->isOpen()) {
        disconnect(mainWindow->getSerialPort(), &QSerialPort::errorOccurred, 0, 0);
        disconnect(mainWindow->getSerialPort(), &QSerialPort::readyRead, 0, 0);
        mainWindow->getSerialPort()->close();
    }
    delete ui;
}

void frmserialport::on_connectButton_clicked()
{
    try {
        qDebug() << "点击了串口连接按钮";

        if (!mainWindow) {
            QMessageBox::critical(this, "程序异常", "主窗口指针无效，操作已中止。");
            return;
        }

        QSerialPort *serialPort = mainWindow->getSerialPort();
        if (!serialPort) {
            QMessageBox::critical(this, "程序异常", "串口对象无效，操作已中止。");
            return;
        }

        if (serialPort->isOpen()) {
            qDebug() << "串口已打开，准备关闭";
            disconnect(serialPort, &QSerialPort::errorOccurred, 0, 0);
            disconnect(serialPort, &QSerialPort::readyRead, 0, 0);
            serialPort->close();
            ui->connectButton->setText("连接");
            ui->enterCommandModeButton->setEnabled(false);
            ui->exitCommandModeButton->setEnabled(false);
            if (mainWindow) {
                enableConfigControls(false);
                mainWindow->setIsSerialConnected(false);
                updateMainResponse("串口已断开");
            }
            qDebug() << "串口已关闭";
        } else {
            QString portName = ui->portComboBox->currentText();
            if (portName.isEmpty()) {
                QMessageBox::critical(this, "程序异常", "请选择一个串口！");
                return;
            }

            int baudRateIndex = ui->baudRateComboBox->currentIndex();
            const int baudRates[] = {9600, 19200, 38400, 57600, 115200};
            qint32 baudRate = baudRates[baudRateIndex];

            serialPort->setPortName(portName);
            serialPort->setBaudRate(baudRate);
            serialPort->setDataBits(QSerialPort::Data8);
            serialPort->setParity(QSerialPort::NoParity);
            serialPort->setStopBits(QSerialPort::OneStop);
            serialPort->setFlowControl(QSerialPort::NoFlowControl);

            if (serialPort->isOpen()) {
                disconnect(serialPort, &QSerialPort::errorOccurred, 0, 0);
                disconnect(serialPort, &QSerialPort::readyRead, 0, 0);
                serialPort->close();
            }

            qDebug() << "准备打开串口";
            bool openResult = serialPort->open(QIODevice::ReadWrite);
            qDebug() << "openResult:" << openResult << "error:" << serialPort->errorString();

            if (openResult) {
                connect(serialPort, &QSerialPort::errorOccurred, this, [this, serialPort](QSerialPort::SerialPortError error) {
                    Q_UNUSED(error);
                    if (mainWindow && mainWindow->getSerialPort() && mainWindow->getSerialPort() == serialPort) {
                        updateMainResponse("串口错误: " + mainWindow->getSerialPort()->errorString());
                        if (mainWindow->getSerialPort()->isOpen()) {
                            disconnect(serialPort, &QSerialPort::errorOccurred, 0, 0);
                            disconnect(serialPort, &QSerialPort::readyRead, 0, 0);
                            mainWindow->getSerialPort()->close();
                            ui->connectButton->setText("连接");
                            ui->enterCommandModeButton->setEnabled(false);
                            ui->exitCommandModeButton->setEnabled(false);
                            enableConfigControls(false);
                            mainWindow->setIsSerialConnected(false);
                            updateMainResponse("串口已断开（错误自动断开）");
                        }
                    }
                });
                connect(mainWindow, &frmmain::serialDataReceived, this, &frmserialport::on_serialDataReceived);
                ui->connectButton->setText("断开");
                ui->enterCommandModeButton->setEnabled(true);
                enableConfigControls(false);
                if (mainWindow) {
                    mainWindow->setIsSerialConnected(true);
                    updateMainResponse("串口 " + portName + " 已连接，波特率: " + QString::number(baudRate));
                }
                qDebug() << "串口打开成功";
            } else {
                QString err = serialPort->errorString();
                QMessageBox::critical(this, "串口打开失败", "无法打开串口 " + portName + "。\n原因：" + err);
                updateMainResponse("错误：无法打开串口 " + portName + ": " + err);
                qDebug() << "串口打开失败:" << err;
            }
        }
        qDebug() << "on_connectButton_clicked 执行结束";
    } catch (const std::exception &ex) {
        QMessageBox::critical(this, "程序异常", QString("发生异常: ") + ex.what());
    } catch (...) {
        QMessageBox::critical(this, "程序异常", "发生未知异常，操作已中止。");
    }
}

void frmserialport::on_enterCommandModeButton_clicked()
{
    if (!mainWindow || !mainWindow->getIsSerialConnected()) {
        updateMainResponse("错误：请先建立串口连接");
        return;
    }

    waitingForCommandModeResponse = true;
    sendCommandModeCommand(true);
    updateMainResponse("已发送进入指令模式命令: AT+TEST");
    commandModeTimer->start(5000);
}

void frmserialport::on_exitCommandModeButton_clicked()
{
    if (!mainWindow || !mainWindow->getIsSerialConnected()) {
        updateMainResponse("错误：请先建立串口连接");
        return;
    }

    sendCommandModeCommand(false);
    isCommandMode = false;
    ui->enterCommandModeButton->setEnabled(true);
    ui->exitCommandModeButton->setEnabled(false);
    enableConfigControls(false);
    updateMainResponse("已退出指令模式");
}

void frmserialport::on_queryModeButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    QString command = "AT+MODE=?\r\n";
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送查询工作模式命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }
}

void frmserialport::on_setModeButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "设置命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    int selectedMode = ui->modeComboBox->currentData().toInt();
    QString modeName = ui->modeComboBox->currentText();
    QString command = QString("AT+MODE=%1\r\n").arg(selectedMode);
    QSerialPort *serialPort = mainWindow->getSerialPort();
    
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse(QString("已发送设置工作模式命令: %1 (%2)").arg(command.trimmed()).arg(modeName));
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_querySelfCheckButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "自检查询命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    waitingForStatusResponse = true;
    QString command = "AT+ESTATUS\r\n";
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送自检查询命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_queryAccelButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "加速度计查询命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    waitingForStatusResponse = true;
    QString command = "AT+ACC=?\r\n";
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送加速度计查询命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_setAccelButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "加速度计设置命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    bool ok;
    double th1 = ui->accelThreshold1LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值1 无效");
        return;
    }
    double th2 = ui->accelThreshold2LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值2 无效");
        return;
    }
    double th3 = ui->accelThreshold3LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值3 无效");
        return;
    }
    double th4 = ui->accelThreshold4LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值4 无效");
        return;
    }

    QString command = QString("AT+ACC=%1,%2,%3,%4\r\n").arg(th1).arg(th2).arg(th3).arg(th4);
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送加速度计设置命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_queryTiltButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "倾角计查询命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    waitingForStatusResponse = true;
    QString command = "AT+ANG=?\r\n";
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送倾角计查询命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_setTiltButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "倾角计设置命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    bool ok;
    double th1 = ui->tiltThreshold1LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值1 无效");
        return;
    }
    double th2 = ui->tiltThreshold2LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值2 无效");
        return;
    }
    double th3 = ui->tiltThreshold3LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值3 无效");
        return;
    }
    double th4 = ui->tiltThreshold4LineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：阈值4 无效");
        return;
    }
    double offsetX = ui->tiltOffsetXLineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：X 初始值无效");
        return;
    }
    double offsetY = ui->tiltOffsetYLineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：Y 初始值无效");
        return;
    }
    double offsetZ = ui->tiltOffsetZLineEdit->text().toDouble(&ok);
    if (!ok) {
        updateMainResponse("错误：Z 初始值无效");
        return;
    }

    QString command = QString("AT+ANG=%1,%2,%3,%4,%5,%6,%7\r\n")
                         .arg(th1).arg(th2).arg(th3).arg(th4)
                         .arg(offsetX).arg(offsetY).arg(offsetZ);
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送倾角计设置命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_queryNtripButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "NTRIP 查询命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    waitingForStatusResponse = true;
    QString command = "AT+NTRIP_SRV=?\r\n";
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送 NTRIP 查询命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::on_setNtripButton_clicked()
{
    if (!isCommandMode) {
        updateMainResponse("错误：请先进入指令模式");
        return;
    }

    static bool isSending = false;
    if (isSending) {
        qDebug() << "NTRIP 设置命令正在发送，忽略重复点击";
        return;
    }
    isSending = true;

    QString srvIp = ui->ntripServerIpLineEdit->text().trimmed();
    if (srvIp.isEmpty()) {
        updateMainResponse("错误：服务器 IP 不能为空");
        return;
    }
    bool ok;
    int srvPort = ui->ntripServerPortLineEdit->text().toInt(&ok);
    if (!ok || srvPort <= 0 || srvPort > 65535) {
        updateMainResponse("错误：服务器端口无效（范围 1-65535）");
        return;
    }
    QString mPoint = ui->ntripMountPointLineEdit->text().trimmed();
    if (mPoint.isEmpty()) {
        updateMainResponse("错误：挂载点不能为空");
        return;
    }
    QString username = ui->ntripUsernameLineEdit->text().trimmed();
    if (username.isEmpty()) {
        updateMainResponse("错误：用户名不能为空");
        return;
    }
    QString password = ui->ntripPasswordLineEdit->text().trimmed();
    if (password.isEmpty()) {
        updateMainResponse("错误：密码不能为空");
        return;
    }

    QString command = QString("AT+NTRIP_SRV=%1,%2,%3,%4,%5\r\n")
                         .arg(srvIp).arg(srvPort).arg(mPoint).arg(username).arg(password);
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        updateMainResponse("已发送 NTRIP 设置命令: " + command.trimmed());
        statusTimer->start(5000);
    } else {
        updateMainResponse("错误：串口未打开");
    }

    QTimer::singleShot(100, this, [this]() { isSending = false; });
}

void frmserialport::updatePorts()
{
    ui->portComboBox->clear();
    foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts()) {
        ui->portComboBox->addItem(info.portName());
    }
}

void frmserialport::sendCommandModeCommand(bool enter)
{
    if (!mainWindow || !mainWindow->getIsSerialConnected()) return;

    QString command = enter ? "AT+TEST\r\n" : "AT+EXIT\r\n";
    QSerialPort *serialPort = mainWindow->getSerialPort();
    if (serialPort->isOpen()) {
        serialPort->write(command.toUtf8());
        serialPort->flush();
        qDebug() << "发送指令模式命令:" << command.trimmed();
    }
}

void frmserialport::enableConfigControls(bool enable)
{
    ui->modeComboBox->setEnabled(enable);
    ui->queryModeButton->setEnabled(enable);
    ui->setModeButton->setEnabled(enable);
    ui->querySelfCheckButton->setEnabled(enable);
    ui->accelThreshold1LineEdit->setEnabled(enable);
    ui->accelThreshold2LineEdit->setEnabled(enable);
    ui->accelThreshold3LineEdit->setEnabled(enable);
    ui->accelThreshold4LineEdit->setEnabled(enable);
    ui->queryAccelButton->setEnabled(enable);
    ui->setAccelButton->setEnabled(enable);
    ui->tiltThreshold1LineEdit->setEnabled(enable);
    ui->tiltThreshold2LineEdit->setEnabled(enable);
    ui->tiltThreshold3LineEdit->setEnabled(enable);
    ui->tiltThreshold4LineEdit->setEnabled(enable);
    ui->tiltOffsetXLineEdit->setEnabled(enable);
    ui->tiltOffsetYLineEdit->setEnabled(enable);
    ui->tiltOffsetZLineEdit->setEnabled(enable);
    ui->queryTiltButton->setEnabled(enable);
    ui->setTiltButton->setEnabled(enable);
    ui->ntripServerIpLineEdit->setEnabled(enable);
    ui->ntripServerPortLineEdit->setEnabled(enable);
    ui->ntripMountPointLineEdit->setEnabled(enable);
    ui->ntripUsernameLineEdit->setEnabled(enable);
    ui->ntripPasswordLineEdit->setEnabled(enable);
    ui->queryNtripButton->setEnabled(enable);
    ui->setNtripButton->setEnabled(enable);
}

void frmserialport::updateMainResponse(const QString &msg)
{
    if (mainWindow) {
        QTextEdit *responseTextEdit = mainWindow->getResponseTextEdit();
        if (responseTextEdit) {
            responseTextEdit->append(msg);
        }
    }
}

void frmserialport::on_serialDataReceived(const QString &data)
{
    qDebug() << "frmserialport 收到串口数据:" << data;

    if (waitingForCommandModeResponse) {
        if (data.contains("CMD_FAIL")) {
            waitingForCommandModeResponse = false;
            commandModeTimer->stop();
            updateMainResponse("错误：进入指令模式失败，设备响应: " + data);
        }
    }

    if (waitingForStatusResponse) {
        waitingForStatusResponse = false;
        statusTimer->stop();
        if (data.trimmed().isEmpty()) {
            updateMainResponse("收到空响应: " + data);
        } else {
            updateMainResponse("设备响应: " + data);
        }
    }
}

void frmserialport::on_commandModeTimeout()
{
    if (waitingForCommandModeResponse) {
        waitingForCommandModeResponse = false;
        isCommandMode = true;
        ui->enterCommandModeButton->setEnabled(false);
        ui->exitCommandModeButton->setEnabled(true);
        enableConfigControls(true);
        updateMainResponse("未收到 CMD_FAIL 响应，假设进入指令模式成功");
    }
}

void frmserialport::on_statusTimeout()
{
    if (waitingForStatusResponse) {
        waitingForStatusResponse = false;
        updateMainResponse("查询失败（5秒超时）");
    }
}